/**
 * @desc 记录操作历史，以便支持和撤销重做功能
 */
export class History {
  constructor() {
    this.undoStack = []
    this.redoStack = []
  }

  record(action) {
    this.undoStack.push(action)
    this.redoStack.length = 0
  }
  // 撤销
  undo() {
    const action = this.undoStack.pop()
    if (action) {
      action.undo()
      this.redoStack.push(action)
    }
  }
  // 重做
  redo() {
    const action = this.redoStack.pop()
    if (action) {
      action.redo()
      this.undoStack.push(action)
    }
  }
}
